package com.clementheliou.cheliou.business.functional.mark.impl;

import static com.clementheliou.cheliou.util.math.average.AverageUtils.getAverageMark;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.clementheliou.cheliou.business.dataaccess.mark.MarkDataAccessService;
import com.clementheliou.cheliou.business.functional.mark.MarkFunctionalService;
import com.clementheliou.cheliou.util.math.average.AverageMark;

/**
 * Default implementation of the {@link MarkFunctionalService} interface.
 * <p>
 * Due to the {@link Transactional} annotation and the value of its
 * <i>readOnly</i> attribute, all methods are called in a transaction that will
 * be rollbacked in the end. This behavior can be overriden at the method level,
 * more particularly if you need to perform an insert, update or delete action.
 * </p>
 * 
 * @author Clément HELIOU (clement.heliou@gmail.com)
 * @see {@link MarkFunctionalService};
 * @see {@link Service};
 * @see {@link Transactional}.
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class MarkDefaultFunctionalService implements MarkFunctionalService {

	/**
	 * Logger for this class.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @see {@link Logger};
	 * @see {@link LoggerFactory}.
	 * @since 1.0
	 */
	private static final Logger LOGGER = LoggerFactory.getLogger(MarkDefaultFunctionalService.class);

	/**
	 * Data access service related to the marks.
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Autowired
	private MarkDataAccessService markDataAccessService;

	/**
	 * <i>{@inheritDoc}</i>
	 * 
	 * @author Clément HELIOU (clement.heliou@gmail.com)
	 * @since 1.0
	 */
	@Override
	public AverageMark getAverageMarkOfPhoto(Integer photoId) {
		LOGGER.debug("Get average mark of the photo {}.", photoId);

		final List<Integer> marks = markDataAccessService.getMarksOfPhoto(photoId);
		return (marks.isEmpty()) ? null : getAverageMark(marks);
	}

}
